package com.stylefeng.guns.rest.modular.film.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.api.film.FilmServiceApi;
import com.stylefeng.guns.api.film.vo.*;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.rest.common.persistence.dao.*;
import com.stylefeng.guns.rest.common.persistence.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Ysh
 */
@Component
@Service(interfaceClass = FilmServiceApi.class)
public class DefaultFilmServiceImpl implements FilmServiceApi {

    @Autowired
    private MoocBannerTMapper moocBannerTMapper;

    @Autowired
    private MoocFilmTMapper moocFilmTMapper;

    @Autowired
    private MoocCatDictTMapper moocCatDictTMapper;

    @Autowired
    private MoocYearDictTMapper moocYearDictTMapper;

    @Autowired
    private MoocSourceDictTMapper moocSourceDictTMapper;

    @Autowired
    private MoocFilmInfoTMapper moocFilmInfoTMapper;

    @Autowired
    private MoocActorTMapper moocActorTMapper;


    @Override
    public List<BannerVO> getBanners() {
        List<MoocBannerT> moocBannerTList=moocBannerTMapper.selectList(null);
        List<BannerVO> result=new ArrayList<>();
        for(MoocBannerT moocBannerT:moocBannerTList){
            BannerVO bannerVO=new BannerVO();
            bannerVO.setBannerId(moocBannerT.getUuid()+"");
            bannerVO.setBannerAddress(moocBannerT.getBannerAddress());
            bannerVO.setBannerUrl(moocBannerT.getBannerUrl());
            result.add(bannerVO);
        }
        return result;
    }


    private List<FilmInfo> getFilmInfos(List<MoocFilmT> moocFilmTS){
        List<FilmInfo> filmInfos=new ArrayList<>();
        for(MoocFilmT moocFilmT:moocFilmTS){
            FilmInfo filmInfo=new FilmInfo();
            filmInfo.setScore(moocFilmT.getFilmScore());
            filmInfo.setImgAddress(moocFilmT.getImgAddress());
            filmInfo.setFilmType(moocFilmT.getFilmType());
            filmInfo.setFilmScore(moocFilmT.getFilmScore());
            filmInfo.setFilmName(moocFilmT.getFilmName());
            filmInfo.setFilmId(moocFilmT.getUuid()+"");
            filmInfo.setExpectNum(moocFilmT.getFilmPresalenum());
            filmInfo.setBoxNum(moocFilmT.getFilmBoxOffice());
            filmInfo.setShowTime(DateUtil.getDay(moocFilmT.getFilmTime()));
            filmInfos.add(filmInfo);
        }
        return  filmInfos;
    }


    @Override
    public FilmVO getHotFilms(boolean isLimit, int nums, int nowPage, int sortId, int sourceId, int yearId, int catId) {
        EntityWrapper<MoocFilmT> entityWrapper=new EntityWrapper<>();
        entityWrapper.eq("film_status","1");
        List<FilmInfo> filmInfos=new ArrayList<>();
        FilmVO filmVO=new FilmVO();
        if(isLimit){
            Page<MoocFilmT> page=new Page<>(1,nums);
            List<MoocFilmT> moocFilmTS = moocFilmTMapper.selectPage(page, entityWrapper);
            filmInfos=getFilmInfos(moocFilmTS);
            filmVO.setFilmNum(moocFilmTS.size());
            filmVO.setFilmInfo(filmInfos);
        }else{
            Page<MoocFilmT> page=null;
            switch (sortId){
                case 1:
                    page=new Page<>(nowPage,nums,"film_box_office");
                    break;
                case 2:
                    page=new Page<>(nowPage,nums,"film_time");
                    break;
                case 3:
                    page=new Page<>(nowPage,nums,"film_score");
                    break;
                default:
                    page=new Page<>(nowPage,nums,"film_box_office");
                    break;
            }
            if(sourceId!=99){
                entityWrapper.eq("film_source",sourceId);
            }
            if(yearId!=99){
                entityWrapper.eq("film_date",yearId);
            }
            if(catId!=99){
                String catStr="%#"+catId+"#%";
                entityWrapper.like("film_cats",catStr);
            }

            List<MoocFilmT> moocFilmTS = moocFilmTMapper.selectPage(page, entityWrapper);
            filmInfos=getFilmInfos(moocFilmTS);
            filmVO.setFilmNum(moocFilmTS.size());
            filmVO.setFilmInfo(filmInfos);

            int totalCount = moocFilmTMapper.selectCount(entityWrapper);
            int totalPage = (totalCount/nums)+1;
            filmVO.setNowPage(nowPage);
            filmVO.setTotalPage(totalPage);
        }
        return filmVO;
    }

    @Override
    public FilmVO getSoonFilms(boolean isLimit, int nums, int nowPage, int sortId, int sourceId, int yearId, int catId) {
        EntityWrapper<MoocFilmT> entityWrapper=new EntityWrapper<>();
        entityWrapper.eq("film_status","2");
        List<FilmInfo> filmInfos=new ArrayList<>();
        FilmVO filmVO=new FilmVO();
        if(isLimit){
            Page<MoocFilmT> page=new Page<>(1,nums);
            List<MoocFilmT> moocFilmTS = moocFilmTMapper.selectPage(page, entityWrapper);
            filmInfos=getFilmInfos(moocFilmTS);
            filmVO.setFilmNum(moocFilmTS.size());
            filmVO.setFilmInfo(filmInfos);
        }else{
            Page<MoocFilmT> page=null;
            switch (sortId){
                case 1:
                    page=new Page<>(nowPage,nums,"film_preSaleNum");
                    break;
                case 2:
                    page=new Page<>(nowPage,nums,"film_time");
                    break;
                case 3:
                    page=new Page<>(nowPage,nums,"film_preSaleNum");
                    break;
                default:
                    page=new Page<>(nowPage,nums,"film_preSaleNum");
                    break;
            }
            if(sourceId!=99){
                entityWrapper.eq("film_source",sourceId);
            }
            if(yearId!=99){
                entityWrapper.eq("film_date",yearId);
            }
            if(catId!=99){
                String catStr="%#"+catId+"#%";
                entityWrapper.like("film_cats",catStr);
            }

            List<MoocFilmT> moocFilmTS = moocFilmTMapper.selectPage(page, entityWrapper);
            filmInfos=getFilmInfos(moocFilmTS);
            filmVO.setFilmNum(moocFilmTS.size());
            filmVO.setFilmInfo(filmInfos);

            int totalCount = moocFilmTMapper.selectCount(entityWrapper);
            int totalPage = (totalCount/nums)+1;
            filmVO.setNowPage(nowPage);
            filmVO.setTotalPage(totalPage);

        }
        return filmVO;
    }

    @Override
    public FilmVO getClassicFilms(int nums, int nowPage, int sortId, int sourceId, int yearId, int catId) {
        EntityWrapper<MoocFilmT> entityWrapper=new EntityWrapper<>();
        entityWrapper.eq("film_status","3");
        List<FilmInfo> filmInfos=new ArrayList<>();
        FilmVO filmVO=new FilmVO();
        Page<MoocFilmT> page=null;
        switch (sortId){
            case 1:
                page=new Page<>(nowPage,nums,"film_box_office");
                break;
            case 2:
                page=new Page<>(nowPage,nums,"film_time");
                break;
            case 3:
                page=new Page<>(nowPage,nums,"film_score");
                break;
            default:
                page=new Page<>(nowPage,nums,"film_box_office");
                break;
        }
        if(sourceId!=99){
            entityWrapper.eq("film_source",sourceId);
        }
        if(yearId!=99){
            entityWrapper.eq("film_date",yearId);
        }
        if(catId!=99){
            String catStr="%#"+catId+"#%";
            entityWrapper.like("film_cats",catStr);
        }

        List<MoocFilmT> moocFilmTS = moocFilmTMapper.selectPage(page, entityWrapper);
        filmInfos=getFilmInfos(moocFilmTS);
        filmVO.setFilmNum(moocFilmTS.size());
        filmVO.setFilmInfo(filmInfos);

        int totalCount = moocFilmTMapper.selectCount(entityWrapper);
        int totalPage = (totalCount/nums)+1;
        filmVO.setNowPage(nowPage);
        filmVO.setTotalPage(totalPage);
        return filmVO;
    }

    @Override
    public List<FilmInfo> getBoxRanking() {
        EntityWrapper<MoocFilmT> entityWrapper=new EntityWrapper<>();
        entityWrapper.eq("film_status","1");
        Page<MoocFilmT> page=new Page<>(1,10,"film_box_office");
        List<MoocFilmT> moocFilms=moocFilmTMapper.selectPage(page,entityWrapper);
        return getFilmInfos(moocFilms);
    }

    @Override
    public List<FilmInfo> getExpectRanking() {
        EntityWrapper<MoocFilmT> entityWrapper=new EntityWrapper<>();
        entityWrapper.eq("film_status","2");
        Page<MoocFilmT> page=new Page<>(1,10,"film_preSaleNum");
        List<MoocFilmT> moocFilms=moocFilmTMapper.selectPage(page,entityWrapper);
        return getFilmInfos(moocFilms);
    }

    @Override
    public List<FilmInfo> getTop() {
        EntityWrapper<MoocFilmT> entityWrapper=new EntityWrapper<>();
        entityWrapper.eq("film_status","1");
        Page<MoocFilmT> page=new Page<>(1,10,"film_score");
        List<MoocFilmT> moocFilms=moocFilmTMapper.selectPage(page,entityWrapper);
        return getFilmInfos(moocFilms);
    }

    @Override
    public List<CatVO> getCats() {
        List<CatVO> cats=new ArrayList<>();
        List<MoocCatDictT> moocCatDictS=moocCatDictTMapper.selectList(null);
        for(MoocCatDictT moocCatDictT:moocCatDictS){
            CatVO catVO = new CatVO();
            catVO.setCatId(moocCatDictT.getUuid()+"");
            catVO.setCatName(moocCatDictT.getShowName());
            cats.add(catVO);
        }
        return cats;
    }

    @Override
    public List<SourceVO> getSources() {
        List<SourceVO> sourceVoList=new ArrayList<>();
        List<MoocSourceDictT> moocSourceDictS = moocSourceDictTMapper.selectList(null);
        for(MoocSourceDictT moocSourceDictT:moocSourceDictS){
            SourceVO sourceVO=new SourceVO();
            sourceVO.setSourceId(moocSourceDictT.getUuid()+"");
            sourceVO.setSourceName(moocSourceDictT.getShowName());
            sourceVoList.add(sourceVO);
        }
        return sourceVoList;
    }

    @Override
    public List<YearVO> getYears() {
        List<YearVO> arrayList=new ArrayList<>();
        List<MoocYearDictT> moocYearDictS=moocYearDictTMapper.selectList(null);
        for(MoocYearDictT moocYearDictT:moocYearDictS){
            YearVO yearVO=new YearVO();
            yearVO.setYearId(moocYearDictT.getUuid()+"");
            yearVO.setYearName(moocYearDictT.getShowName());
            arrayList.add(yearVO);
        }
        return arrayList;
    }

    @Override
    public FilmDetailVO getFilmDetail(int searchType, String searchParam) {
        FilmDetailVO filmDetailVO=null;
        if(searchType==1){
            filmDetailVO = moocFilmTMapper.getFilmDetailByName("%"+searchParam+"%");
        }else{
            filmDetailVO = moocFilmTMapper.getFilmDetailById(searchParam);
        }

        return filmDetailVO;
    }


    private MoocFilmInfoT getFilmInfo(String filmId){
        MoocFilmInfoT moocFilmInfoT=new MoocFilmInfoT();
        moocFilmInfoT.setFilmId(filmId);
        moocFilmInfoT=moocFilmInfoTMapper.selectOne(moocFilmInfoT);
        return moocFilmInfoT;
    }

    @Override
    public FilmDescVO getFilmDesc(String filmId) {
        MoocFilmInfoT moocFilmInfoT = getFilmInfo(filmId);
        FilmDescVO filmDescVO=new FilmDescVO();
        filmDescVO.setBiography(moocFilmInfoT.getBiography());
        filmDescVO.setFilmId(moocFilmInfoT.getFilmId());
        return filmDescVO;
    }

    @Override
    public ImgVO getImgs(String filmId) {
        MoocFilmInfoT moocFilmInfoT = getFilmInfo(filmId);
        String filmImgStr = moocFilmInfoT.getFilmImgs();
        String[] filmImgS = filmImgStr.split(",");
        ImgVO imgVO=new ImgVO();
        imgVO.setMainImg(filmImgS[0]);
        imgVO.setImg01(filmImgS[1]);
        imgVO.setImg02(filmImgS[2]);
        imgVO.setImg03(filmImgS[3]);
        imgVO.setImg04(filmImgS[4]);
        return imgVO;
    }

    @Override
    public ActorVO getDectInfo(String filmId) {
        MoocFilmInfoT moocFilmInfoT = getFilmInfo(filmId);
        Integer directorId = moocFilmInfoT.getDirectorId();
        MoocActorT moocActorT = moocActorTMapper.selectById(directorId);
        ActorVO actorVO=new ActorVO();
        actorVO.setImgAddress(moocActorT.getActorImg());
        actorVO.setDirectorName(moocActorT.getActorName());
        return actorVO;
    }

    @Override
    public List<ActorVO> getActors(String filmId) {
        List<ActorVO> actors = moocActorTMapper.getActors(filmId);
        return actors;
    }
}
